package com.example.expandablelistview.multimedia;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

import com.example.expandablelistview.R;

public class ChangeVolume extends Activity {
	MediaPlayer mPlayer;
	AudioManager mAutioManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mul_changevolume);
		mPlayer = new MediaPlayer();
		String sd = Environment.getExternalStorageDirectory().getAbsolutePath();
		try {
			mPlayer.setDataSource(sd + "/oldpop.mp3");
			mPlayer.prepare();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		mAutioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		SeekBar seekBar = (SeekBar) findViewById(R.id.seekvolume);
		seekBar.setMax(mAutioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
		seekBar.setProgress(mAutioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
		seekBar.setOnSeekBarChangeListener(mOnSeek);
	}
	
	OnSeekBarChangeListener mOnSeek = new OnSeekBarChangeListener() {
		public void onStopTrackingTouch(SeekBar seekBar) {}
		public void onStartTrackingTouch(SeekBar seekBar) {}
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			mAutioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_ALLOW_RINGER_MODES);
		}
	};
	
	protected void onPause() {
		super.onPause();
		if (mPlayer !=null){
			mPlayer.release();
			mPlayer = null;
		}
	}

	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.btnplay:
			if (mPlayer.isPlaying()){
				mPlayer.pause();
			}else{
				mPlayer.start();
			}
			break;
		case R.id.btnvoldown:
			mAutioManager.adjustStreamVolume(
					AudioManager.STREAM_MUSIC, 
					AudioManager.ADJUST_LOWER, 
					AudioManager.FLAG_SHOW_UI |
					AudioManager.FLAG_ALLOW_RINGER_MODES |
					AudioManager.FLAG_PLAY_SOUND |
					AudioManager.FLAG_VIBRATE
					);
			break;
		case R.id.btnvolup:
			mAutioManager.adjustStreamVolume(
					AudioManager.STREAM_MUSIC, 
					AudioManager.ADJUST_RAISE, 
					AudioManager.FLAG_SHOW_UI |
					AudioManager.FLAG_ALLOW_RINGER_MODES |
					AudioManager.FLAG_PLAY_SOUND |
					AudioManager.FLAG_VIBRATE
					);
			break;
		case R.id.btnring:
			switch(mAutioManager.getRingerMode()){
			case AudioManager.RINGER_MODE_NORMAL:
				Toast.makeText(this, "벨소리 모드", Toast.LENGTH_LONG).show();
				mAutioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
				Toast.makeText(this, "무음 모드", Toast.LENGTH_LONG).show();
				break;
			case AudioManager.RINGER_MODE_SILENT:
				Toast.makeText(this, "무음 모드", Toast.LENGTH_LONG).show();
				mAutioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
				Toast.makeText(this, "진동 모드", Toast.LENGTH_LONG).show();
				break;
			case AudioManager.RINGER_MODE_VIBRATE:
				Toast.makeText(this, "진동 모드", Toast.LENGTH_LONG).show();
				mAutioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
				Toast.makeText(this, "벨소리 모드", Toast.LENGTH_LONG).show();
				break;
			}
			break;
		case R.id.btneffect1:
			mAutioManager.playSoundEffect(AudioManager.FX_KEY_CLICK);
			break;
		case R.id.btneffect2:
			mAutioManager.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
			break;
		case R.id.btneffect3:
			mAutioManager.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
			break;
		}
	}
}














